import threading
import time
'''
线程数据同步
lock和死锁
'''
# 自定义锁
lock=threading.Lock()
num=0
def task1():
    global num
    for i in range(1000000):
        #如果锁没有获取到，则一直等待锁
       lock.acquire()
       num+=1
       lock.release()
    pass
def task2():
    global num
    for i in range(1000000):
        lock.acquire()
        num+=1
        lock.release()
    pass

# 死锁，两个对象互相持有对方的锁，就容易产生死锁
lockA=threading.Lock()
lockB=threading.Lock()
class thread1(threading.Thread):
    def run():
        pass

class thread2(threading.Thread):
    def run():
        pass

if __name__ == '__main__':
    t1=threading.Thread(target=task1)
    t2=threading.Thread(target=task2)
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print(num)